﻿@page "/admin/listmessages"
@attribute [Authorize(Roles = "Admin")]

@inject IHttpService _httpService
@inject IPopupService PopupService
@inject NavigationManager NavigationManager

<CnGalWebSite.ProjectSite.Shared.Components.Shared.TitleCard Title="消息版块" />

<MDCard>
    <DataTableCard TModel="MessageOverviewModel" Headers="_headers" ApiUrl="@(_baseUrl)" Title="@($"{_name}列表")" ShowAddButton="false" @ref="dataTableCard">
        <ItemColContent>
            @if (context.Header.Value == "actions")
            {
                <div style="width: 140px;">
                    <MIcon Color="success"
                           Class="mr-2"
                           OnClick="()=>Read(context.Item)">mdi-share-all-outline</MIcon>
                        @if (context.Item.Hide)
                    {
                        <MIcon Color="warning"
                               Class="mr-2"
                               OnClick="()=>HideItem(context.Item,false)">@IconType.Check.ToIconString()</MIcon>
                    }
                    else
                    {
                        <MIcon Color="error"
                               Class="mr-2"
                               OnClick="()=>HideItem(context.Item,true)">@IconType.Close.ToIconString()</MIcon>
                    }
                </div>
            }
            else if (context.Header.Value == nameof(MessageOverviewModel.Hide))
            {
                <CnGalWebSite.ProjectSite.Shared.Components.Admin.Projects.HideChip Hide="context.Item.Hide" />
                <CnGalWebSite.ProjectSite.Shared.Components.Admin.Messages.ReadChip Read="context.Item.Read" />

            }
            else if (context.Header.Value == nameof(MessageOverviewModel.UserId))
            {
                <CnGalWebSite.ProjectSite.Shared.Components.Admin.Users.UserChip Id="@context.Item.UserId" Name="@context.Item.UserName" />
            }
            else if (context.Header.Value == nameof(MessageOverviewModel.CreateTime))
            {
                @context.Item.CreateTime.ToString("yyyy-MM-dd HH:mm")
            }
            else if (context.Header.Value == nameof(MessageOverviewModel.Text))
            {
                <div style="white-space: pre-line;width: 300px;word-break: break-all;">
                    @context.Item.Text
                </div>
            }
            else
            {
                @context.Value
            }
        </ItemColContent>
    </DataTableCard>

</MDCard>


@code {
    private string _baseUrl = "api/message/List";
    private string _name = "消息";

    private List<DataTableHeader<MessageOverviewModel>> _headers = new List<DataTableHeader<MessageOverviewModel>>
{
          new ()
          {
            Text= "Id",
            Align= DataTableHeaderAlign.Start,
            Value= nameof(MessageOverviewModel.Id)
          },
          new (){ Text= "时间", Value= nameof(MessageOverviewModel.CreateTime)},
          new (){ Text= "用户", Value= nameof(MessageOverviewModel.UserId)},
          new (){ Text= "内容", Value= nameof(MessageOverviewModel.Text)},
          new (){ Text= "状态", Value= nameof(MessageOverviewModel.Hide)},
          new (){ Text= "操作", Value= "actions",Sortable=false }
    };

    DataTableCard<MessageOverviewModel> dataTableCard;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    public void Read(MessageOverviewModel model)
    {
        if (model.PageType == PageType.Project)
        {
            NavigationManager.NavigateTo($"/project/{model.PageId}");
        }
        else if (model.PageType == PageType.Stall)
        {
            NavigationManager.NavigateTo($"/stall/{model.PageId}");
        }
    }

    public async Task HideItem(MessageOverviewModel model, bool isHidden)
    {
        string operationName = isHidden ? "隐藏评论" : "显示评论";
        try
        {
            var obj = await _httpService.PostAsync<HideModel, Result>("api/message/Hide", new HideModel { Id = model.Id, Hide = isHidden });

            if (obj.Success == false)
            {
                await PopupService.ToastAsync(operationName + "失败", AlertTypes.Error);
            }
            else
            {

                await PopupService.ToastAsync(operationName + "成功", AlertTypes.Success);
                dataTableCard.GetDataFromApi();

            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, operationName + "失败");
        }
    }
}
